<?php

namespace App\Manage;


use App\Models\SiteInfo;
use App\Models\User;
use App\Models\UserAuth;
use App\Service\Base\Manage;

class StudentManage extends Manage
{

    // 基本配置
    public function initialize()
    {
        parent::initialize();

        $this->model = 'Student';
    }

    // 列表
    // 添加
    // 修改
    // 软删除

    // 列表查询
    protected function listWhere()
    {
        $option  = $this->option();
        $where   = [];
        $bang    = [];
        $c_id    = $option->getData('course_id', 0, 'intval');
        $uid     = $option->getData('uid', 0, 'intval');
        $age     = $option->getData('age', null, 'intval');
        $name    = $option->getData('person_name', null, 'trim');
        $mobile  = $option->getData('mobile', null, 'trim');
        $keyword = $option->getData('keyword', null, 'trim');
        if($c_id){
            $bang[]  = 'course_id';
            $where[] = ['course_id', '=', $c_id];
        }
        if($uid){
            $bang[]  = 'uid';
            $where[] = ['uid', '=', $uid];
        }
        if($age){
            $bang[]  = 'age';
            $where[] = ['age', '=', $age];
        }
        if($name){
            $bang    = false; // 设置不在缓存
            $where[] = ['person_name', 'like', "%".$name."%"];
        }
        if($mobile){
            $bang    = false; // 设置不在缓存
            $where[] = ['mobile', 'like', "%".$mobile."%"];
        }
        if($keyword){
            $bang = false; // 设置不在缓存
            //$where[] = $this->orWhere('username|nickname|mobile', 'like', $keyword);
        }

        // 缓存
        $this->bang = $bang;

        return $where;
    }


    // 添加之前
    protected function _before_add()
    {
        $opt = $this->option();
        $uid = $opt->getData("uid", null, "intval");
        // 获取认证编号
        $user = (new User())->isCache(true)->oneFind(['id' => $uid]);
        if(empty($user)){
            return $this->_returnFail('你选择的用户不存在！');
        }
        if(empty($user['is_auth'])){
            return $this->_returnFail('你选择的用户没有认证！');
        }
        if(empty($user['is_auth'])){
            return $this->_returnFail('你选择的用户没有认证！');
        }
        if($user['verify'] != 1){
            return $this->_returnFail('你选择的用户认证未通过！');
        }

        $auth_id = $user['auth_id'];
        $data    = [
            //'id'          => $opt->getData("id", null, "intval"),
            'uid'         => $uid,
            'auth_id'     => $auth_id,
            'person_name' => $opt->getData("person_name", null, "trim"),
            'head_img'    => $opt->getData("head_img", null, "trim"),
            'sex'         => $opt->getData("sex", null, "intval"),
            'age'         => $opt->getData("age", null, "intval"),
            'mobile'      => $opt->getData("mobile", null, "trim"),
            'guardor'     => $opt->getData("guardor", null, "trim"),
            'is_remind'   => $opt->getData("is_remind", null, "intval"),
            'status'      => $opt->getData("status", 0, "intval"),
            //'ctime'       => $opt->getData("ctime", null, "intval"),
            //'mtime'       => $opt->getData("mtime", null, "intval"),
        ];

        return $data;
    }

    // 修改之前
    protected function _before_edit()
    {
        $opt  = $this->option();
        $uid  = $opt->getData("uid", null, "intval");
        $a_id = $opt->getData("auth_id", null, "intval");
        if(empty($uid) && empty($a_id)){
            return $this->_returnFail('请指定所属机构！');
        }

        // 获取认证编号
        if($a_id && empty($uid)){
            $auth = (new UserAuth())->isCache(true)->oneFind(['id' => $a_id]);
            if(empty($auth)){
                return $this->_returnFail('你选择的用户不存在！');
            }
            $uid = $auth['uid'];
        }

        // 获取用户信息
        $user = (new User())->isCache(true)->oneFind(['id' => $uid]);
        if(empty($user)){
            return $this->_returnFail('你选择的用户不存在！');
        }
        if(empty($user['is_auth'])){
            return $this->_returnFail('你选择的用户没有认证！');
        }
        if(empty($user['is_auth'])){
            return $this->_returnFail('你选择的用户没有认证！');
        }
        if($user['verify'] != 1){
            return $this->_returnFail('你选择的用户认证未通过！');
        }

        $auth_id = $user['auth_id'];
        $data    = [
            //'id'          => $opt->getData("id", null, "intval"),
            'uid'         => $uid,
            'auth_id'     => $auth_id,
            'person_name' => $opt->getData("person_name", null, "trim"),
            'head_img'    => $opt->getData("head_img", null, "trim"),
            'sex'         => $opt->getData("sex", null, "intval"),
            'age'         => $opt->getData("age", null, "intval"),
            'mobile'      => $opt->getData("mobile", null, "trim"),
            'guardor'     => $opt->getData("guardor", null, "trim"),
            'is_remind'   => $opt->getData("is_remind", null, "intval"),
            'status'      => $opt->getData("status", 0, "intval"),
            //'ctime'       => $opt->getData("ctime", null, "intval"),
            //'mtime'       => $opt->getData("mtime", null, "intval"),
        ];

        return $data;
    }

}


